package com.my.app.tools.ui;

import com.google.common.collect.Lists;
import com.my.app.tools.core.business.CoreBusiness;
import com.my.app.tools.pojo.vo.PeopleInfoVO;
import com.my.app.tools.util.AppThreadUtil;
import com.my.app.tools.util.LogUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

/**
 * ui操作
 *
 * @author guochaohui
 * @return
 * @date 2020-03-16 10:17
 */
public class AppUiMakePeopleInfo {

    private static JPanel mainPanel;

    private static JPanel controlPanel;
    private static JLabel makeCountLabel;
    private static JComboBox makeCountComboBox;
    private static JLabel genderLabel;
    private static JComboBox genderComboBox;
    private static JLabel bankTypeLabel;
    private static JComboBox bankTypeComboBox;
    private static JButton makeButton;

    private static JPanel resultPanel;
    private static JScrollPane resultScrollPane;
    private static JTable resultTable;

    /**
     * 初始化UI
     *
     * @return
     * @author guochaohui
     * @date 2020-03-16 10:20
     */
    public static final JPanel InitUi() {

        // 所有控件面板
        mainPanel = new JPanel(null);
        mainPanel.setBounds(0, 0, AppUiMain.appWindowWidth, AppUiMain.appWindowHeight);

        // 所有控件
        controlPanel = new JPanel(null);
        controlPanel.setBounds(10, 10, 610, 60);
        controlPanel.setBorder(BorderFactory.createTitledBorder("控制按钮"));

        makeCountLabel = new JLabel("生成数量");
        makeCountLabel.setBounds(30, 20, 80, 20);
        controlPanel.add(makeCountLabel);

        makeCountComboBox = new JComboBox(new Object[]{20, 50, 100});
        makeCountComboBox.setBounds(90, 22, 50, 20);
        controlPanel.add(makeCountComboBox);

        genderLabel = new JLabel("性别");
        genderLabel.setBounds(170, 20, 60, 20);
        controlPanel.add(genderLabel);

        genderComboBox = new JComboBox(new Object[]{"任意", "男", "女"});
        genderComboBox.setBounds(205, 22, 60, 20);
        controlPanel.add(genderComboBox);

        bankTypeLabel = new JLabel("银行类型");
        bankTypeLabel.setBounds(300, 20, 80, 20);
        controlPanel.add(bankTypeLabel);

        bankTypeComboBox = new JComboBox(CoreBusiness.bankTypes());
        bankTypeComboBox.setBounds(360, 22, 90, 20);
        controlPanel.add(bankTypeComboBox);

        makeButton = new JButton("生成数据");
        makeButton.setBounds(480, 22, 100, 20);
        makeButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                makeData();
            }
        });
        controlPanel.add(makeButton);
        // 数据结果
        resultPanel = new JPanel(null);
        resultPanel.setBounds(10, 70, 610, 370);
        resultPanel.setBorder(BorderFactory.createTitledBorder("数据结果-点击左键选择复制"));

        // 解析结果列表
        resultTable = new JTable(new Vector(), new Vector(Lists.newArrayList("序号", "姓名", "性别", "年龄", "出生日期", "身份证号", "手机号", "银行卡号", "银行名称", "银行缩写")));
        resultTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        resultTable.setBounds(10, 22, 590, 340);
        resultTable.getTableHeader().setResizingAllowed(false);
        resultTable.getTableHeader().setReorderingAllowed(false);
        resultTable.setRowHeight(20);
        resultTable.setPreferredScrollableViewportSize(new Dimension(590, 340));
        resultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        AppUiMain.setTableColumnWidth(resultTable, Lists.newArrayList(40, 60, 0, 0, 0, 135, 85, 140, 55, 55));
        resultTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                AppUiMain.clickTableShowCopyMenu(e, resultTable);
            }
        });
        resultScrollPane = new JScrollPane(resultTable);
        resultScrollPane.setBounds(10, 22, 590, 340);
        resultPanel.add(resultScrollPane);

        mainPanel.add(controlPanel);
        mainPanel.add(resultPanel);
        return mainPanel;
    }

    /**
     * 生成数据
     *
     * @return
     * @author guochaohui
     * @date 2020-04-01 16:53
     */
    private static void makeData() {
        AppUiMain.showLoading("数据生成中，请稍候......");
        AppThreadUtil.INSTANCE.getExecutor().submit(() -> {
            try {
                Object makeCountObject = makeCountComboBox.getModel().getSelectedItem();
                Integer generateCount = Integer.valueOf(String.valueOf(makeCountObject));
                int gender = genderComboBox.getSelectedIndex() - 1;
                int bankType = bankTypeComboBox.getSelectedIndex() - 1;
                List<PeopleInfoVO> list = CoreBusiness.generatePeopleInfo(generateCount, gender, bankType);
                Vector<Vector> datas = new Vector<>();
                for (int i = 0; i < list.size(); i++) {
                    Vector vector = new Vector(Lists.newArrayList(
                            // 序号
                            i + 1
                            // 姓名
                            , list.get(i).getPeopleName()
                            // 性别
                            , list.get(i).getPeopleGender()
                            // 年龄
                            , list.get(i).getPeopleAge()
                            // 生日
                            , list.get(i).getPeopleBrithday()
                            // 证件号
                            , list.get(i).getPeopleIdCardNo()
                            // 手机号
                            , list.get(i).getPeopleMobile()
                            // 银行卡号
                            , list.get(i).getPeopleBankCard().getCardNo()
                            // 银行类型
                            , list.get(i).getPeopleBankCard().getCnName()
                            // 银行缩写
                            , list.get(i).getPeopleBankCard().getEnName()
                    ));
                    datas.add(vector);
                }
                AppUiMain.setTableData(resultTable, datas, false);
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.error(e.getMessage());
                AppUiMain.errorMsgBox(e.getMessage());
            } finally {
                AppUiMain.hideLoading();
            }
        });
    }

}
